package com.fubang.pathlearning.path.rankview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.fubang.pathlearning.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RankView extends View {

    private Paint mPaint, mChartPaint;//普通画笔，柱形图画笔
    private Rect mBound;//纵坐标数字区域
    private int mHeight, mWidth, mChartStartWidth, mChartSize;//总宽高
    private int topColor, bottomColor, selectedColor;
    private List<Float> list = new ArrayList<>();
    private getNumberListener listener;
    private int number;
    private int selectedIndex = -1;
    //圆角的半径，依次为左上角xy半径，右上角，右下角，左下角
    private float[] radius = {30.0f, 30.0f, 30.0f, 30.0f, 0.0f, 0.0f, 0.0f, 0.0f,};
    private int max = 60;


    public RankView(Context context) {
        this(context, null);
    }

    public RankView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RankView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RankView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            if (attr == R.styleable.RankView_topColor) {
                topColor = array.getColor(attr, Color.BLACK);

            } else if (attr == R.styleable.RankView_bottomColor) {
                bottomColor = array.getColor(attr, Color.BLACK);

            } else if (attr == R.styleable.RankView_selectedColor) {
                selectedColor = array.getColor(attr, Color.BLACK);

            } else {
                bringToFront();
            }
        }
        array.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = widthSize / 2;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = heightSize / 2;
        }

        setMeasuredDimension(width, height);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mBound = new Rect();
        mChartPaint = new Paint();
        mChartPaint.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();
        mChartSize = getWidth() / 20;
        mChartStartWidth = getWidth() / 11 - mChartSize / 2;//10个点，11个间隔，求得起始位置
        drawOrdinate(canvas);//循环绘制纵坐标
        drawHistogram(canvas);//循环绘制柱形图 and 选中时候的三角形
    }

    private void drawOrdinate(Canvas canvas) {
        for (int i = 6; i >= 0; i--) {
            mPaint.setTextSize(35);
            int i1 = (max) / 6 * (6 - i);
            mPaint.getTextBounds(String.valueOf(i1) + "", 0, String.valueOf(i1).length(), mBound);
            canvas.drawText(String.valueOf(i1), mChartStartWidth - mBound.width() - 10, ((mHeight - mBound.height()) / 6) * i + mBound.height(), mPaint);
        }
    }

    private void drawHistogram(Canvas canvas) {
        boolean selected;//标记是否点击选中
        for (int i = 0; i < list.size(); i++) {
            int percent = mHeight / max;//每个柱形高度的占比
            mChartPaint.setStyle(Paint.Style.FILL);
            if (list.size() > 0) {
                if (selectedIndex == i) {
                    mChartPaint.setShader(null);
                    mChartPaint.setColor(selectedColor);
                    selected = true;
                } else {
                    selected = false;
                    LinearGradient lg = new LinearGradient(0, 0, 0, mHeight, bottomColor, topColor, Shader.TileMode.MIRROR);
                    mChartPaint.setShader(lg);
                }
                //画柱状图
                Path path = new Path();
                RectF rectF = new RectF();
                rectF.left = mChartStartWidth;
                rectF.right = mChartStartWidth + mChartSize;
                rectF.bottom = mHeight;
                float v = list.get(i) * percent;
                if (v != 0) {
                    v = v < 30 ? 30 : v;
                }
                rectF.top = mHeight - v;
                path.addRoundRect(rectF, radius, Path.Direction.CCW);
                canvas.drawPath(path, mChartPaint);
                if (selected) {
                    //画一个实心三角形
                    Paint paint = new Paint();
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.WHITE);
                    Path path2 = new Path();
                    path2.moveTo(mChartStartWidth + mChartSize / 2 - 10, mHeight);
                    path2.lineTo(mChartStartWidth + mChartSize / 2, mHeight - 10);
                    path2.lineTo(mChartStartWidth + mChartSize / 2 + 10, mHeight);
                    path2.close();
                    canvas.drawPath(path2, paint);
                }
                mChartStartWidth += getWidth() / 11;
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int x = (int) ev.getX();
        int y = (int) ev.getY();
        int left = mWidth / 11 - mChartSize / 2;
        int top = 0;
        int right = mWidth / 11 + mChartSize / 2;
        int bottom = mHeight;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                for (int i = 0; i < 10; i++) {
                    Rect rect = new Rect(left, top, right, bottom);
                    left += mWidth / 11;
                    right += mWidth / 11;
                    if (rect.contains(x, y)) {
                        number = i;
                        selectedIndex = i;
                        invalidate();
                    }
                }
                listener.getNumber(number, x, y);
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }


    public void setList(List<Float> list) {
        this.list = list;
        Float max = Collections.max(list);
        if (max > 60) {
            int remainder = (int) (max / 30);
            this.max = remainder == 0 ? remainder * 30 : (remainder + 1) * 30;//以30为间隔的递增算法
        }
        invalidate();
    }

    public void setListener(getNumberListener listener) {
        this.listener = listener;
    }

    interface getNumberListener {
        void getNumber(int number, int x, int y);
    }
}